package com.yidian.controller.business;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yidian.entity.Result;
import com.yidian.pojo.user.User;
import com.yidian.server.user.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * @author whb
 * @create 2021-07-08 17:35
 * @desciption 用户控制类
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    /**
     * 保存短信验证码（redis）和把验证码作为消息发送到rabbitmq
     * @param phoneNo
     * @return
     */
    @GetMapping("/sendSms")
    public Result sendSms(String phoneNo){
        userService.sendSms(phoneNo);
        return new Result(0,"短信验证码发送成功！");
    }

    /**
     * 注册用户
     * @param user
     * @param smsCode
     * @return
     */
    @RequestMapping("/register")
    public Result register(@RequestBody User user,String smsCode){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String newPassword = encoder.encode(user.getPassword());//使用BCrypt加密密码
        user.setPassword(newPassword);
        userService.register(user,smsCode);
        return new Result(0,"用户注册成功");
    }
}
